package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.base.BaseResult;
import com.ls.base.BaseResultUtil;
import com.ls.controller.model.channel.AddChannelModel;
import com.ls.controller.model.channel.EditChannelModel;
import com.ls.controller.model.channel.QueryChannelModel;
import com.ls.controller.model.channel.SendSmsWithChannelModel;
import com.ls.entity.CommChannel;
import com.ls.entity.SysUser;
import com.ls.exception.SysException;
import com.ls.mapper.CommChannelMapper;
import com.ls.service.ICommChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import static com.ls.base.BaseResultUtil.SQL_ERROR;

/**
 * <p>
 * 上游通道表，不做每分钟的字段限制，这个统一在后台代码中限制 服务实现类
 * </p>
 * @author pengyu
 * @since 2019-12-05
 */
@Service
public class CommChannelServiceImpl extends ServiceImpl<CommChannelMapper, CommChannel> implements ICommChannelService {

    @Autowired
    CommChannelMapper channelMapper;

    @Override
    public BaseResult add(AddChannelModel model, SysUser currentUser) {
        boolean checkFlag = checkChannelNameExist(model.getChanName());
        if (checkFlag) {
            return BaseResultUtil.error("通道名称已存在");
        }
        CommChannel commChannel = new CommChannel();
        BeanUtils.copyProperties(model, commChannel);
        commChannel.setCreateTime(LocalDateTime.now());
        commChannel.setCreateUserId(currentUser.getUserId());
        commChannel.setUpdateTime(LocalDateTime.now());
        commChannel.setUpdateUserId(currentUser.getUserId());
        int result = channelMapper.insert(commChannel);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    @Override
    public boolean checkChannelNameExist(String chanName) {
        if (StringUtils.isBlank(chanName)) {
            throw new SysException("checkChannelNameExist 需要参数");
        }
        CommChannel commChannel = channelMapper.selectOne(new QueryWrapper<CommChannel>().eq("chan_name", chanName));
        if (commChannel == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public BaseResult edit(EditChannelModel model, SysUser currentUser) {
        Long chanId = model.getChanId();
        CommChannel commChannel = channelMapper.selectById(chanId);
        if (commChannel == null) {
            throw new SysException("chanId: "+ chanId +" 在库里不存在");
        }
        String chanNameNew = model.getChanName();
        if (!commChannel.getChanName().equals(chanNameNew)) {
            boolean commChannelCheck = checkChannelNameExist(chanNameNew);
            if (commChannelCheck) {
                throw new SysException(chanNameNew + " 在库里已经存在");
            }
        }
        BeanUtils.copyProperties(model, commChannel);
        commChannel.setUpdateTime(LocalDateTime.now());
        commChannel.setUpdateUserId(currentUser.getUserId());
        int result = channelMapper.updateById(commChannel);
        if (result > 0) {
            return BaseResultUtil.success();
        } else {
            return BaseResultUtil.error(SQL_ERROR);
        }
    }

    @Override
    public BaseResult listByPage(QueryChannelModel model, SysUser currentUser) {
        QueryWrapper<CommChannel> wrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(model.getChanName())) {
            wrapper.like("chan_name", model.getChanName());
        }
        if (model.getChanStatus() != null) {
            wrapper.eq("chan_status", model.getChanStatus());
        }
        if (model.getOpenStatus() != null) {
            wrapper.eq("open_status", model.getOpenStatus());
        }
        IPage<CommChannel> pageData = channelMapper.selectPage(new Page<>(model.getPageNum(), model.getPageSize()), wrapper);
        return BaseResultUtil.success(pageData);
    }

    @Override
    public BaseResult testSendSms(SendSmsWithChannelModel model, SysUser currentUser) {
        return null;
    }

}
